from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.action_chains import ActionChains
# 1. 手动指定 ChromeDriver 路径
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec

# 页面等待
service = Service('E:/devtools/Python/Python312/chromedriver.exe')  # 替换为实际路径
chrome = webdriver.Chrome(service=service)

try:
    # chrome.get("https:/www.baidu.com/")
    # 隐式等待
    # chrome.implicitly_wait(5)
    # chrome.find_element(By.ID, 'abc')
    # 显示等待
    chrome.get('https://kyfw.12306.cn/otn/leftTicket/init?linktypeid=dc')
    WebDriverWait(driver=chrome, timeout=100).until(
        ec.text_to_be_present_in_element_value((By.ID, 'fromStationText'), '上海')
    )
    WebDriverWait(driver=chrome, timeout=100).until(
        ec.text_to_be_present_in_element_value((By.ID, 'toStationText'), '北京')
    )
    btn = chrome.find_element(By.ID, 'query_ticket')
    btn.click()
    input("回车键关闭浏览器...")

except Exception as e:
    print(f"执行过程中出错: {e}")
finally:
    chrome.quit()
